package com.ascentive.finallyfast.deepclean;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.ascentive.finallyfast.FinallyFastApplication;
import com.ascentive.finallyfast.R;

public class DeepCleanView extends LinearLayout implements Button.OnClickListener, OnCheckedChangeListener {

	// categories in Deep Clean feature
	public static final int CALL_LOG = 0;
    public static final int SMS = 1;
    public static final int BROWSER_HISTORY = 2;
    public static final int CLIPBOARD = 3;
    public static final int MARKET_SEARCH_HISTORY = 4;
    public static final int YOUTUBE_SEARCH_HISTORY = 5;
    public static final int GMAIL_SEARCH_HISTORY = 6;
    public static final int GOOGLE_SEARCH_HISTORY = 7;
    public static final int GOOGLE_MAPS_SEARCH_HISTORY = 8;
    public static final int GOOGLE_EARTH_SEARCH_HISTORY = 9;
    public static final int ALL_APP_CACHE = 10;
   
    // Categories to select duration for clearing junk files
    public static final int DAY = 0;
    public static final int WEEK = 1;
    public static final int MONTH = 2;
    
	private FinallyFastApplication mApp = null;
	private DeepCleanViewListener mListener = null;
	
	TextView mTotalSpace = null;
	TextView mAvailSpace = null;
	ExpandableListView mExpandableListView = null;
	Button mClearSelectedBtn = null;
	CheckBox mCheckAllBox = null;
	
	public DeepCleanView(FinallyFastApplication app) {
		super(app);
		mApp = app;
		inflate();
	}

	private void inflate(){
		View.inflate(mApp, R.layout.deep_clean_list, this);
		
		mTotalSpace = (TextView)findViewById(R.id.total_space);
		mTotalSpace.setText("Total:" + Utility.megabytesTotal() + "MB");
		
		mAvailSpace = (TextView)findViewById(R.id.avail_space);
		// mAvailSpace.setBackgroundColor(Color.WHITE);
		mAvailSpace.setTextColor(Color.BLACK);
		mAvailSpace.setText("Free:" + Utility.megabytesAvailable() + "MB");
	    
		mExpandableListView = (ExpandableListView)findViewById(R.id.deepCleanPage_listView);
		
		Log.v(null, "Found list View**********");
		mClearSelectedBtn = (Button)findViewById(R.id.button);
		mClearSelectedBtn.setOnClickListener(this);
		mCheckAllBox = (CheckBox)findViewById(R.id.chkAll);
		mCheckAllBox.setOnCheckedChangeListener(this);
	}
	
	public void setAdapter(DeepCleanListExpandableAdapter adapter){
		if(mExpandableListView != null)
			mExpandableListView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}
	
	public void setListener(DeepCleanViewListener listener){
		mListener = listener;
	}

	@Override
	public void onClick(View v) {
		if(v.equals(mClearSelectedBtn))
			mListener.onClearSelectedButtonPressed();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(buttonView.equals(mCheckAllBox)) // && isChecked == true)
			{
			//Handle the generic check box 
			mListener.onCheckBoxPressed(isChecked);
		}else{
			if(isChecked == false && mCheckAllBox.isChecked() == true){
				mCheckAllBox.setChecked(false);
			}
		}
	}
	
	public void uncheckCommonChkBox(){
		mCheckAllBox.setChecked(false);
	}    
	
	public void expandAllGroups()
	{
		mExpandableListView.expandGroup(0);
		mExpandableListView.expandGroup(1);
	}
	
	public void refreshMemoryStatus()
	{
		mAvailSpace.setText("Free:" + Utility.megabytesAvailable() + "MBBB");
	}
	
}
